package com.smart;

import java.util.ArrayList;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;

import com.smart.data.Contact;
import com.smart.data.ContactSearchAccessor;
import com.smart.task.InitTask;
import com.smart.task.GenericTask;
import com.smart.task.MatchTask;
import com.smart.task.TaskListener;
import com.smart.task.TaskListenerAdapter;
import com.smart.task.TaskParam;
import com.smart.task.TaskResult;

public class ContactSearchActivity extends Activity  implements TextWatcher{
	private static final String TAG = "SmartDialeActivity" ;
	
	private static final boolean DEBUG = false;
	
	private static final boolean INIT_ON_BACKGROUND = false;
	private static final boolean MATCH_ON_BACKGROUND = false;
	
	private EditText mDigits ;
	private ListView mMatchedList ;
	private TaskListener mMatchListener ;
	private TaskListener mInitListener ;
	private GenericTask mMatchTask ;
	private GenericTask mInitTask ;
	
	private String filterStr = "";
	
	private boolean  matchQueued = false ;
	private boolean  isInitied = false ;
	
	private ContactsListAdapter mAdapter ;
	
	private ContactSearchAccessor accessor ;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_contact);
        initView() ;
        initListener()  ;
        mAdapter = new ContactsListAdapter(this, new ArrayList<Contact>());
        mMatchedList.setAdapter(mAdapter);
        if( accessor == null)  {
        	accessor = ContactSearchAccessor.getInstance(this) ;
        }
        if(INIT_ON_BACKGROUND){
        	doInitMemory() ;
        }else{
        	accessor.init() ;
        	isInitied = true ;
        }
    }
    
    
    @Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}

	public void initView() {
    	mDigits = (EditText) findViewById(R.id.digits) ;
    	mMatchedList = (ListView) findViewById(R.id.matched_list) ;
	}
    
    public void initListener() {
    	mDigits.addTextChangedListener(this);
    	
    	mMatchListener = new TaskListenerAdapter() {
			
			@Override
			public String getName() {
				// TODO Auto-generated method stub
				return "mMatchListener"; 
			}

			@Override
			public void onPreExecute(GenericTask task) {
				// TODO Auto-generated method stub
				super.onPreExecute(task);
				if(DEBUG) Log.v(TAG, "mMatchListener  >> onPreExecute") ;
			}

			@Override
			public void onPostExecute(GenericTask task, TaskResult result) {
				// TODO Auto-generated method stub
				super.onPostExecute(task, result);
				if(DEBUG) Log.v(TAG, "mMatchListener  >> onPostExecute ,result.data.size = " + result.data.size()) ;
				mAdapter.update(result.data, result.filter) ;
				if(matchQueued)	doMatch() ;
			}

			@Override
			public void onCancelled(GenericTask task) {
				// TODO Auto-generated method stub
				super.onCancelled(task);
				if(matchQueued)	doMatch() ;
			}
			
		};
		
		mInitListener = new TaskListenerAdapter() {
			
			@Override
			public String getName() {
				// TODO Auto-generated method stub
				return "mInitListener";
			}

			@Override
			public void onPreExecute(GenericTask task) {
				// TODO Auto-generated method stub
				super.onPreExecute(task);
				if(DEBUG) Log.v(TAG, "mInitListener  >> onPreExecute ") ;
			}

			@Override
			public void onPostExecute(GenericTask task, TaskResult result) {
				// TODO Auto-generated method stub
				super.onPostExecute(task, result);
				isInitied = true ;
				if(DEBUG) Log.v(TAG, "mInitListener  >> onPostExecute, isInitied="+isInitied) ;
				if(matchQueued){
					doMatch() ;
				}
			}

			@Override
			public void onCancelled(GenericTask task) {
				// TODO Auto-generated method stub
				super.onCancelled(task);
				isInitied = false ;
			}
			
		};
	}
    
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		filterStr = s.toString() ;
		if(MATCH_ON_BACKGROUND){
			doMatch() ;
		}else{
			mAdapter.update(accessor.startQuery(filterStr), filterStr) ; ;
		}
	}
	
	private synchronized void doMatch(){
		if(isInitied){
			if( mMatchTask != null && mMatchTask.getStatus() == GenericTask.Status.RUNNING){
				matchQueued = true ;
				//mMatchTask.cancel(true) ;
			}else{
				matchQueued = false ;
				TaskParam param = new TaskParam() ;
				param.filter = filterStr;
				mMatchTask  = new MatchTask() ;
				mMatchTask.setCancelable(false) ;
				mMatchTask.accessor = accessor ;
				mMatchTask.setTaskListener(mMatchListener);
				mMatchTask.execute(param) ;
			}
		}else{
			matchQueued = true ;
			doInitMemory() ;
		}
		
	}
	
	private void doInitMemory(){
		if(isInitied){
			return ;
		}else{
			if(mInitTask != null && mInitTask.getStatus() == GenericTask.Status.RUNNING){
				return ;
			}else{
				mInitTask  = new InitTask() ;
				mInitTask.setCancelable(false) ;
				mInitTask.accessor = accessor ;
				mInitTask.setTaskListener(mInitListener);
				mInitTask.execute() ;
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}

}

